18. Exercise: OnClickListener
L1 42 OnClickListener SC
In this exercise, you will make your app show a Toast message every time the button is clicked.
1. Make sure that you've completed the steps to findViewById:
You'll need to findViewById for the button, as described in the last video.
3. Set the OnClickListener for the button:
rollButton.setOnClickListener {
}
4. Make the Toast:
This code will make the rollButton show a Toast message.
rollButton.setOnClickListener {
Toast.makeText(this, "button clicked", Toast.LENGTH_SHORT).show()
}
If you want to start at this step, you can download this exercise code here: Step.02-Exercise-OnClickListener.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here Step.02-Solution-OnClickListener or using this git diff
Task Description:
Check the steps below as you implement them to complete this exercise.